<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>onUserInputChange</title>
</head>

<body>
    <p>请在移动端设备中使用手触摸页面，并打开控制台!</p>
    <script>
        const onUserInputChange = callback => {
            let type = 'mouse',
                lastTime = 0;
            const mouseMoveHandler = () => {
                const now = performance.now();
                if (now - lastTime < 20) {
                    type = 'mouse';
                    callback(type);
                    document.removeEventListener('mousemove', mouseMoveHandler);
                }
                lastTime = now;
            }
            document.addEventListener('touchstart', () => {
                if (type === 'touch') {
                    return;
                }
                type = 'touch';
                callback(type);
                document.addEventListener('mousemove', mouseMoveHandler);
            })
        }
        onUserInputChange(type => {
            console.log('The user is now using', type, 'as an input method.');
        });
    </script>
</body>

</html>